package com.top.jwtdemo.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.top.jwtdemo.entity.User;
import com.top.jwtdemo.mapper.UserMapper;
import com.top.jwtdemo.utils.LoginProtectionFilter;
import com.top.jwtdemo.utils.R;
import com.top.jwtdemo.utils.ResponseEnum;
import com.top.jwtdemo.utils.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author  Cjj
 * @data  2024/6/17 13:55
 * @blog  控制器
 */
@RestController

public class LoginController {

    private static final int MAX_ATTEMPS = 3;//最多登录尝试次数
    private static final Map<String,Integer> loginAttemps = new HashMap<>();

     @Resource
    UserMapper userMapper;
     @PostMapping("/login")
     public User login(@RequestBody User user){
         LoginProtectionFilter loginProtectionFilter = new LoginProtectionFilter();
         boolean isUserBlocked   = loginProtectionFilter.isBlocked(user.getName());
         if (isUserBlocked) {
             // 用户被阻止登录
             System.out.println("用户 " + user + " 被阻止登录。");
         } else {
             // 用户可以正常登录
             System.out.println("用户 " + user + " 可以正常登录。");
         }
         return userMapper.getUserByNameAndPassword(user.getName(),user.getPassword());

     }
    // 调用地图API获取附近的充电站
    @GetMapping("/nearbyChargingStations")
    public R getNearbyChargingStations(@RequestParam("latitude") double latitude,
                                       @RequestParam("longitude") double longitude) {
        // 模拟获取附近充电站的逻辑，这里只是返回一个固定的结果
        String nearbyChargingStations = "{\"chargingStations\":[{\"name\":\"充电站1\",\"address\":\"地址1\"}," +
                "{\"name\":\"充电站2\",\"address\":\"地址2\"}]}";

        return R.ok(nearbyChargingStations);
     }

}
